package com.gdth.bank.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.greendao.query.QueryBuilder;

import com.gdth.bank.BankApplication;
import com.gdth.bank.event.MessageEvent;
import com.gdth.database.bean.DaoSession;

import com.gdth.database.bean.NotificationT;

import com.gdth.database.bean.NotificationTDao;
import com.gdth.http.method.HttpMethodServlet;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import rx.Observable;
import rx.Subscriber;

public class NotificationModel {
	
	/**
	 * 查询用户所有通知
	 * @param val
	 * @return
	 */
	public Observable<List<NotificationT>> getAllDataUuid(final String sfz){
		return Observable.create(new Observable.OnSubscribe<List<NotificationT>>() {

			@Override
			public void call(Subscriber<? super List<NotificationT>> arg0) {
				// TODO 查询当前采访客户的所有附件
				NotificationTDao dao = BankApplication.getDaoSession().getNotificationTDao();
				QueryBuilder<NotificationT> qb = dao.queryBuilder();
				qb.whereOr(NotificationTDao.Properties.Jsruuid.like(sfz), NotificationTDao.Properties.Jsruuid.like("")).orderDesc(NotificationTDao.Properties.XxtsId);
//				qb.where(NotificationDao.Properties.jsruuid.like(sfz)).orderDesc(NotificationDao.Properties.xxtsId);
				qb.where(NotificationTDao.Properties.Xxzt.le(2)).orderDesc(NotificationTDao.Properties.XxtsId);
				List<NotificationT> mList = qb.list();
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			}
		});
	}
	
	/**
	 * 查询未读数据
	 * @param val
	 * @return
	 */
	public Observable<List<NotificationT>> getNoReadDataUuid(final String sfz){
		return Observable.create(new Observable.OnSubscribe<List<NotificationT>>() {

			@Override
			public void call(Subscriber<? super List<NotificationT>> arg0) {
				// TODO 查询当前采访客户的所有附件
				NotificationTDao dao = BankApplication.getDaoSession().getNotificationTDao();
				QueryBuilder<NotificationT> qb = dao.queryBuilder();
				qb.whereOr(NotificationTDao.Properties.Jsruuid.like(sfz), NotificationTDao.Properties.Jsruuid.like("")).orderDesc(NotificationTDao.Properties.XxtsId);
//				qb.where(NotificationDao.Properties.jsruuid.like(sfz)).orderDesc(NotificationDao.Properties.xxtsId);
				qb.where(NotificationTDao.Properties.Xxzt.eq(1)).orderDesc(NotificationTDao.Properties.XxtsId);
				List<NotificationT> mList = qb.list();
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			}
		});
	}
	
	/**
	 * 查询已读数据
	 * @param val
	 * @return
	 */
	public Observable<List<NotificationT>> getReadDataUuid(final String sfz){
		return Observable.create(new Observable.OnSubscribe<List<NotificationT>>() {

			@Override
			public void call(Subscriber<? super List<NotificationT>> arg0) {
				// TODO 查询当前采访客户的所有附件
				NotificationTDao dao = BankApplication.getDaoSession().getNotificationTDao();

				QueryBuilder<NotificationT> qb = dao.queryBuilder();
				qb.whereOr(NotificationTDao.Properties.Jsruuid.like(sfz), NotificationTDao.Properties.Jsruuid.like("")).orderDesc(NotificationTDao.Properties.XxtsId);
//				qb.where(NotificationDao.Properties.jsruuid.like(sfz)).orderDesc(NotificationDao.Properties.xxtsId);
				qb.where(NotificationTDao.Properties.Xxzt.eq(2)).orderDesc(NotificationTDao.Properties.XxtsId);
				List<NotificationT> mList = qb.list();
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			}
		});
	}
	
	/**
	 * 查询标题搜索
	 * @param val
	 * @return
	 */
	public Observable<List<NotificationT>> getTitleDataUuid(final String sfz, final String key){
		return Observable.create(new Observable.OnSubscribe<List<NotificationT>>() {

			@Override
			public void call(Subscriber<? super List<NotificationT>> arg0) {
				// TODO 查询当前采访客户的所有附件
				NotificationTDao dao = BankApplication.getDaoSession().getNotificationTDao();
				QueryBuilder<NotificationT> qb = dao.queryBuilder();
				qb.whereOr(NotificationTDao.Properties.Jsruuid.like(sfz), NotificationTDao.Properties.Jsruuid.like("")).orderDesc(NotificationTDao.Properties.XxtsId);
//				qb.where(NotificationDao.Properties.jsruuid.like(sfz)).orderDesc(NotificationDao.Properties.xxtsId);
				qb.where(NotificationTDao.Properties.Xxzt.le(2)).orderDesc(NotificationTDao.Properties.XxtsId);
				qb.whereOr(NotificationTDao.Properties.Tzbt.like("%"+key+ "%"), NotificationTDao.Properties.Tznr.like("%"+key+ "%")).orderDesc(NotificationTDao.Properties.XxtsId);
				List<NotificationT> mList = qb.list();
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			}
		});
	}
	
	/**
	 * 查询标题搜索
	 * @param val
	 * @return
	 */
	public Observable<List<NotificationT>> getContentDataUuid(final String sfz, final String key){
		return Observable.create(new Observable.OnSubscribe<List<NotificationT>>() {

			@Override
			public void call(Subscriber<? super List<NotificationT>> arg0) {
				// TODO 查询当前采访客户的所有附件
				NotificationTDao dao = BankApplication.getDaoSession().getNotificationTDao();
				QueryBuilder<NotificationT> qb = dao.queryBuilder();
				qb.whereOr(NotificationTDao.Properties.Jsruuid.like(sfz), NotificationTDao.Properties.Jsruuid.like("")).orderDesc(NotificationTDao.Properties.XxtsId);
//				qb.where(NotificationDao.Properties.jsruuid.like(sfz)).orderDesc(NotificationDao.Properties.xxtsId);
				qb.where(NotificationTDao.Properties.Xxzt.le(2),NotificationTDao.Properties.Tznr.like("%"+key+ "%")).orderDesc(NotificationTDao.Properties.XxtsId);
				List<NotificationT> mList = qb.list();
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			}
		});
	}
	 
	
	/**
	 * 修改本地消息状态， 标记为已经已读
	 * @param mList
	 */
	public  void  readNotification(int xxid) {
		DaoSession session = BankApplication.getDaoSession();
		NotificationTDao dao = session.getNotificationTDao();
		QueryBuilder<NotificationT> query = dao.queryBuilder();
		query.where(NotificationTDao.Properties.XxtsId.eq(xxid));
		List<NotificationT> mLists = query.list();
		if(mLists.size() > 0) {
			mLists.get(0).setXxzt(2);
		}
		dao.insertOrReplace(mLists.get(0));
		
		EventBus.getDefault().post(new MessageEvent(MessageEvent.NOTIFICATION_GET, String.valueOf(xxid)));
	}
	
	/**
	 * 获取网路最新通知
	 * @param content --  身份zhen
	 * @return
	 */
	public Observable<String> NotificationList(String sfz,String lastXXid) {
		 Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();  
		 Map<String, Object> map = new HashMap<String, Object>();  
		 map.put("jsruuid", sfz);
		 map.put("xxid", lastXXid);
		 String content = gson2.toJson(map) ;
		return HttpMethodServlet.getInstance().notificationList(content);
	}
	
	

	/**
	 * 删除单条通知
	 * @param content --  身份zhen
	 * @return
	 */
	public Observable<String> DelNotification(String sfz,String XXid) {
		 Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();  
		 Map<String, Object> map = new HashMap<String, Object>();  
		 map.put("jsruuid", sfz);
		 map.put("xxid", XXid);
		 String content = gson2.toJson(map) ;
		return HttpMethodServlet.getInstance().delNotificationList(content);
	}


	/**
	 * 删除多条通知
	 * @param content --  身份zhen
	 * @return
	 */
	public Observable<String> DelMoreNotification(String sfz,	 List<Map> delXXIDs) {
		 Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();  
//		 String delContent = gson2.toJson(xxids);
//		 final String delContent = gson2.toJson(delXXIDs);
		 Map<String, Object> map = new HashMap<String, Object>();  
		 map.put("jsruuid", sfz);
		 map.put("list", delXXIDs);
		 String content = gson2.toJson(map) ;
		return HttpMethodServlet.getInstance().delNotificationList(content);
	}



	/**
	 * 修改本地消息状态， 标记为已经删除
	 * @param
	 */
	public  Observable<String>  delLocalNotification(final List<NotificationT> xxid) {
		
		return Observable.create(new Observable.OnSubscribe<String>() {

			@Override
			public void call(Subscriber<? super String> arg0) {
				// TODO 查询当前采访客户的所有附件
				DaoSession session = BankApplication.getDaoSession();
				NotificationTDao dao = session.getNotificationTDao();
				QueryBuilder<NotificationT> query = dao.queryBuilder();
//				query.where(NotificationDao.Properties.xxtsId.eq(xxid));
				List<NotificationT> mLists = query.list();
				for(int i = 0 ; i < mLists.size() ; i ++) {
						for(int j = 0 ; j <xxid.size() ; j ++ ) {
							if(mLists.get(i).getXxtsId() == xxid.get(j).getXxtsId())
								mLists.get(i).setXxzt(3);
						}
				}
				
				dao.insertOrReplaceInTx(mLists);
				EventBus.getDefault().post(new MessageEvent(MessageEvent.NOTIFICATION_GET,""));
				
			}
	});
	}
	
	/**
	 * 修改本地消息状态， 标记为已经删除
	 * @param
	 */
	public  Observable<String>  changeLocalNotification(final int xxid) {
		
		return Observable.create(new Observable.OnSubscribe<String>() {

			@Override
			public void call(Subscriber<? super String> arg0) {
				// TODO 查询当前采访客户的所有附件
				DaoSession session = BankApplication.getDaoSession();
				NotificationTDao dao = session.getNotificationTDao();
				QueryBuilder<NotificationT> query = dao.queryBuilder();
				query.where(NotificationTDao.Properties.XxtsId.eq(xxid));
				List<NotificationT> mLists = query.list();
				if(mLists.size() > 0) {
					mLists.get(0).setXxzt(2);
				}
				dao.insertOrReplace(mLists.get(0));
				EventBus.getDefault().post(new MessageEvent(MessageEvent.NOTIFICATION_GET,""));
				
			}
	});
	}
	
}
